﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.UC
{
    public partial class SanPhamHotCtrl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                List<SAN_PHAM> ltSanPham = SanPhamModels.LaySanPhamMuaNhieu();
                Content.InnerHtml = GenerateSPTieuBieu(ltSanPham);
            }
            
        }
        private string GenerateSPTieuBieu(List<SAN_PHAM> ltSanPham)
        {
            StringBuilder strBuilder = new StringBuilder();
            
            int count = 0;
            strBuilder.Append("<div class='box-content'>");
    strBuilder.Append("<div class='box-products slide' id='productc1'>");
        strBuilder.Append("<div class='carousel-controls'><a class='carousel-control left' data-slide='prev' href='#productc1'><i class='fa fa-angle-left fa-fw'></i></a><a class='carousel-control right' data-slide='next' href='#productc1'><i class='fa fa-angle-right fa-fw'></i></a></div>");
        strBuilder.Append("<div class='carousel-inner'>");
            if(ltSanPham!=null)
            {
                for (int j = 0; j < ltSanPham.Count(); j++)
                {
                    if (j == 0)
                    {
                        strBuilder.Append("<div class='item active'>");
                        strBuilder.Append("<div class='row box-product'>");
                    }
                    else if (count == 0)
                    {
                        strBuilder.Append("<div class='item'>");
                        strBuilder.Append("<div class='row box-product'>");
                    }
                    count++;

                    strBuilder.Append("<div class='col-lg-4 col-md-4 col-sm-4 col-xs-12'>");
                    strBuilder.Append("<div class='product-block'>");
                    strBuilder.Append("<div class='image'>");
                    strBuilder.Append("<div class='product-label product-hot'><span>HOT</span></div>");
                    strBuilder.Append("<a class='img' href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID + "'>");
                    strBuilder.Append("<img alt='product info' src='images/products/" + ltSanPham[j].HINH_SAN_PHAM.First().Hinh + "' title='product title'> ");
                    strBuilder.Append("</a>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("<div class='product-meta'>");
                    strBuilder.Append("<div class='name'><a href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID + "'>" + ltSanPham[j].TenSanPham + "</a></div>");
                    if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                    {
                        strBuilder.Append("<div class='big-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                 "{0:0,0}", (ltSanPham[j].DonGia - ((ltSanPham[j].DonGia * ltSanPham[j].GiamGia) / 100))) + "<span class='sym'>đ</span></span></div>");
                    }
                    else
                    {
                        strBuilder.Append("<div class='big-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                 "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span></div>");
                    }
                    strBuilder.Append("<div class='big-btns'> <a class='btn btn-default btn-view pull-left' href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID + "'>Xem</a> <button class='btn btn-default btn-addtocart pull-right' onclick='ThemVaoGioHang(" + ltSanPham[j].ID + ")'>Thêm</button> </div>");
                    if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                    {
                        strBuilder.Append("<div class='small-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                     "{0:0,0}", (ltSanPham[j].DonGia - ((ltSanPham[j].DonGia * ltSanPham[j].GiamGia) / 100))) + "<span class='sym'>đ</span></span> ");
                        strBuilder.Append("<span class='price-old'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span> </div>");
                    }
                    else
                    {
                        strBuilder.Append("<div class='small-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                 "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span></div>");
                    }
                    strBuilder.Append(Utils.GenerateRation(ltSanPham[j].DANH_GIA.ToList()));
                    strBuilder.Append("<div class='small-btns'>");
                    strBuilder.Append("<button class='btn btn-default btn-compare pull-left' data-toggle='tooltip' title='Add to Compare' onclick='Compare(" + ltSanPham[j].ID + ")'> <i class='fa fa-retweet fa-fw'></i> </button>");
                    strBuilder.Append("<button class='btn btn-default btn-wishlist pull-left' data-toggle='tooltip' title='Add to Wishlist' onclick='ThemVaoYeuThich(" + ltSanPham[j].ID + ")'> <i class='fa fa-heart fa-fw'></i> </button>");
                    strBuilder.Append("<button class='btn btn-default btn-addtocart pull-left' data-toggle='tooltip' title='Add to Cart' onclick='ThemVaoGioHang(" + ltSanPham[j].ID + ")'> <i class='fa fa-shopping-cart fa-fw'></i> </button>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("<div class='meta-back'></div>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");

                    if (count == 3 || j == ltSanPham.Count() - 1)
                    {
                        strBuilder.Append("</div>");
                        strBuilder.Append("</div>");
                        count = 0;
                    }


                }
            }
            
            strBuilder.Append("</div>");
            strBuilder.Append("</div>");
            strBuilder.Append("</div>");
            return strBuilder.ToString();
        }
    }
}